package com.dai.zhenfujin.visitingcard.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.EaseConversationAdapter2;
import com.dai.zhenfujin.bean.BlackBean;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.db.Constant;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.message.ChatActivity;
import com.dai.zhenfujin.view.RoundImageView;
import com.daimajia.swipe.SwipeLayout;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MsgLisstAdapter extends BaseAdapter {
    private Context context;
    private List<BlackBean.FollowListBean> beanList;
    private LayoutInflater inflater;
    private List<SwipeLayout> swipeLayoutList = new ArrayList<>();
    private EMConversation conversation;

    public MsgLisstAdapter(Context context, List<BlackBean.FollowListBean> beanList) {
        this.context = context;
        this.beanList = beanList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new MsgLisstAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.ease_row_chat_history,null);

            holder.name = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.name);
            holder.unreadLabel = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.unread_msg_number);
            holder.message = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.message);
            holder.time = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.time);
            holder.tv_user_job = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.tv_user_job);
            holder.avatar = (ImageView) convertView.findViewById(com.hyphenate.easeui.R.id.avatar);
            holder.msgState = convertView.findViewById(com.hyphenate.easeui.R.id.msg_state);
            holder.list_itease_layout = (RelativeLayout) convertView.findViewById(com.hyphenate.easeui.R.id.list_itease_layout);
            holder.motioned = (TextView) convertView.findViewById(com.hyphenate.easeui.R.id.mentioned);
            holder.swipeLayout = convertView.findViewById(com.hyphenate.easeui.R.id.swipe_layout);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BlackBean.FollowListBean bean = beanList.get(position);

        holder.name.setText(bean.user_name);
        String nickName2 = SPUtils.getConversionNickName(context, bean.friend_id);
        if (!TextUtils.isEmpty(nickName2)){
            holder.name.setText(nickName2);
        }else {
            holder.name.setText("系统消息");
        }
        RequestOptions options = new RequestOptions().placeholder(com.hyphenate.easeui.R.drawable.logo);
            Glide.with(context).load(bean.user_logo)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop())).apply(options).into(holder.avatar);
        SPUtils.saveConversionNickName(context, bean.friend_id,bean.user_name);
        SPUtils.saveConversionHeadPhoto(context, bean.friend_id, bean.user_logo);

        conversation = EMClient.getInstance().chatManager().getConversation(bean.friend_id);
        if (null == conversation) {
            conversation = EMClient.getInstance().chatManager().getConversation(bean.friend_id, EMConversation.EMConversationType.Chat, true);
        }


        //进入聊天界面
        holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  SwipeLayout swipeLayout = swipeLayoutList.get(position);
                if (swipeLayout.getOpenStatus() == SwipeLayout.Status.Open) {
                    // return;
                }*/
                Intent intent = new Intent(context, ChatActivity.class);
                    // it's single chat
                    intent.putExtra(Constant.EXTRA_USER_ID, bean.friend_id);

                    context.startActivity(intent);
            }
        });

        convertView.findViewById(com.hyphenate.easeui.R.id.bottom_wrapper).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> params = new HashMap<>();
                Map<String, String> params2 = new HashMap<>();
                params2.put("friend_id", bean.friend_id);
                params.put("req_bd", params2);
                HttpClient.post(context, Constants.DELCHATLIST, params, new CallBack<BlackBean>() {
                    @Override
                    public void onSuccess(BlackBean resultBean) {
                        EMClient.getInstance().chatManager().deleteConversation(bean.friend_id, true);
                        beanList.remove(position);
                        notifyDataSetChanged();
                    }
                });
            }
        });


        int msgSize = conversation.getUnreadMsgCount();
        if (msgSize > 0){
            holder.unreadLabel.setVisibility(View.VISIBLE);
            holder.unreadLabel.setText(msgSize + "");
        }else {
            holder.unreadLabel.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }


    class ViewHolder{
        /**
         * who you chat with
         */
        TextView name;
        /**
         * unread message count
         */
        TextView unreadLabel;
        /**
         * content of last message
         */
        TextView message;
        /**
         * time of last message
         */
        TextView time;
        TextView tv_user_job;
        /**
         * avatar
         */
        ImageView avatar;
        /**
         * status of last message
         */
        View msgState;
        /**
         * layout
         */
        RelativeLayout list_itease_layout;
        TextView motioned;
        SwipeLayout swipeLayout;
    }
}
